/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <rom.prevot@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return R. Prevot
 * ----------------------------------------------------------------------------
 */

/******************************************************************************\
 *          ____                _______    _____ _             _              *
 *         |  _ \              |__   __|  / ____| |           | |             *
 *         | |_) | ___  _ __ _ __ | | ___| (___ | |_ ___   ___| | __          *
 *         |  _ < / _ \| '__| '_ \| |/ _ \\___ \| __/ _ \ / __| |/ /          *
 *         | |_) | (_) | |  | | | | | (_) |___) | || (_) | (__|   <           *
 *         |____/ \___/|_|  |_| |_|_|\___/_____/ \__\___/ \___|_|\_\          *
 *                                                                            *
\******************************************************************************/

package app.controllers;

import app.core.UpdatedComp;
import app.events.ClientListener;
import app.events.eventobjects.ClientAddedEvent;
import app.events.eventobjects.ClientDeletedEvent;
import app.events.eventobjects.ClientUpdatedEvent;
import app.models.Client;
import app.models.Factory;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.EventListenerList;

/**
 *
 * @author Ghost
 */
public class Registre {

    ArrayList<Client> clients;
    private EventListenerList listenerList = new EventListenerList();

    public Registre() {
        clients = new ArrayList<Client>();
    }

    public void load(UpdatedComp comp) {
        comp.setMax(-1);
        List l = Factory.getContext().createNamedQuery("Client.findAll").getResultList();
        comp.setMax(l.size());
        int i = 0;
        for (Object o : l) {
            comp.setCurrent(i++);
            Client cli = (Client) o;
            Factory.persist(cli);
            comp.setText("Chargement du client " + cli.getId());
            addClient(cli);
        }
    }

    public ArrayList<Client> getClients() {
        return clients;
    }

    /**
     * Add a client to the context
     */
    public void saveClient(Client cli) {
        Factory.save(cli);
        addClient(cli);
    }
        
    public void updateClient(Client oldcli, Client newCli) {
        Client dbCli = Factory.getContext().find(Client.class, oldcli.getId());
        dbCli.setNom(newCli.getNom());
        dbCli.setPrenom(newCli.getPrenom());
        dbCli.setEmail(newCli.getEmail());
        dbCli.setTel(newCli.getTel());
        dbCli.setVenteList(newCli.getVenteList());
        dbCli.setCreated(newCli.getCreated());
        
        Factory.save(dbCli);
        
        int i = clients.indexOf(oldcli);
        clients.set(i, dbCli);
        
        fireClientUpdated(new ClientUpdatedEvent(oldcli, dbCli, this));
    }
    
    public void removeClient(Client cli){
        Factory.remove(cli);
        fireClientDeleted(new ClientDeletedEvent(cli, this));
    }
    
    /**
     * Private method to add a client and spread the info
     * @param cli 
     */
    private void addClient(Client cli) {
        clients.add(cli);
        fireClientAdded(new ClientAddedEvent(cli, this));
    }

    public void addClientListener(ClientListener list) {
        listenerList.add(ClientListener.class, list);
    }

    public void removeClientListener(ClientListener l) {
        listenerList.remove(ClientListener.class, l);
    }

    public void fireClientAdded(ClientAddedEvent cae) {
        Object[] listeners = listenerList.getListenerList();
        for (int i = 0; i < listeners.length; i += 2) {
            if (listeners[i] == ClientListener.class) {
                ((ClientListener) listeners[i + 1]).onClientAdded(cae);
            }
        }
    }

    public void fireClientDeleted(ClientDeletedEvent cde) {
        Object[] listeners = listenerList.getListenerList();
        for (int i = 0; i < listeners.length; i += 2) {
            if (listeners[i] == ClientListener.class) {
                ((ClientListener) listeners[i + 1]).onClientDeleted(cde);
            }
        }
    }

    public void fireClientUpdated(ClientUpdatedEvent cue) {
        Object[] listeners = listenerList.getListenerList();
        for (int i = 0; i < listeners.length; i += 2) {
            if (listeners[i] == ClientListener.class) {
                ((ClientListener) listeners[i + 1]).onClientUpdated(cue);
            }
        }
    }
}
